package com.jwdf.uda.ui.fragment.question.index;

import android.annotation.SuppressLint;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.ViewNetErrorBinding;
import com.jwdf.uda.event.FollowEvent;
import com.jwdf.uda.event.QuizNetErrorEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.action.RecommandAction;
import com.jwdf.uda.ui.adapter.QuizRecommandAdapter;
import com.jwdf.uda.utils.AppBus;
import com.jwdf.uda.utils.ConfigUtils;
import com.squareup.otto.Subscribe;

import java.util.List;

import retrofit2.Response;

/**
 * Created by fatsfan on 2016/9/8.
 */
@SuppressLint("ValidFragment")
public class QuizConcernFragment extends QuizIndexFragment {

    private QuizRecommandAdapter adapter;

    public QuizConcernFragment() {
        restType = PbBean.PbSignBaseQuestionType.QUESTION_INDEX_CONCERN;
        setTitle("关注的人");
    }

    @Override
    protected void initViews() {
        super.initViews();
        layEmpty = mDataBinding.layRecommand;
        RecyclerView lv = mDataBinding.lvRecommand;
        lv.setLayoutManager(new LinearLayoutManager(aty));
        lv.setHasFixedSize(true);
        adapter = new QuizRecommandAdapter(this);
        lv.setAdapter(adapter);

        mDataBinding.vConcernRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                showProgressDialog();
                requestDataRecommand();
            }
        });

        requestDataRecommand();

        //register , unregister , post
        AppBus.getInstance().register(this);
    }

    @Override
    protected void doSuccessResponse(boolean refresh, PbBean.PbResQuestionQuestionList body) {
        super.doSuccessResponse(refresh, body);

        if (ConfigUtils.getFollowCount(getActivity()) > 0) {
            final List<PbBean.PbBaseQuestion> questionInfoList = body.getQuestionInfoList();

            StringBuffer strBoss = new StringBuffer();
            for (PbBean.PbBaseQuestion item : questionInfoList) {
                final String actionStr = item.getActionStr();
                if (actionStr != null && actionStr.length() != 0)
                    strBoss.append(actionStr);
            }
            if (strBoss.length() == 0)
                mDataBinding.tvEmpty.setText("你关注的人暂时还没有相关操作哦~");

        }
    }

    void requestDataRecommand() {
        RecommandAction action = new RecommandAction(aty);
        onNewRequestCall(action.request(new AHttpService.IResCallback<PbBean.PbResUserGetRandRecommendUserList>() {

            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResUserGetRandRecommendUserList> response, PbBean.PbBaseServerInfo baseServerInfo) {
//                closeProgressDialog();
                if (response != null) {
                    adapter.setData(response.body());
                }
            }
        }));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        AppBus.getInstance().unregister(this);
    }

    @Subscribe
    public void onChanged(FollowEvent event) {
        requestData(true);
        requestDataRecommand();
    }
    public void onEventMainThread(QuizNetErrorEvent event) {
        ViewNetErrorBinding errorBinding = DataBindingUtil.bind(mErrorView);
        errorBinding.failedLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onReload(v.getContext());
            }
        });
        if (errorBinding.getRoot().getParent() == null) {
            mDataBinding.quizContainer.addView(errorBinding.getRoot());
        }
    }
    @Override
    protected void onReload(Context context) {
        requestData(true);
        requestDataRecommand();
    }

}
